/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.webos.java.base;

import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.util.Base64;
import org.urchr.web.db.DBUtil;
import org.webos.java.base.fs.FileManager;
import org.webos.java.base.users.DBUserProvider;
import org.webos.java.base.UserManager;
import org.webos.java.base.NetworkManager;

/**
 *
 * @author kostya
 */
public class RootFilter implements Filter {

    private FilterConfig filterConfig = null;
    private ServletContext servletContext = null;

    public RootFilter() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String servletPath = req.getServletPath();
//        Log.debug(this, "servlet path: "+servletPath);
        if(servletPath.startsWith("/cache/")){
            servletPath = servletPath.substring(7);
//            Log.debug(this, "cache file: "+servletPath);
            servletContext.getRequestDispatcher(Constants.CACHE_FS_PATH+servletPath).forward(request, response);
            return;
        }
        if(servletPath.startsWith("/res/") && servletPath.indexOf("/", 6)!=-1){
            String appName = servletPath.substring(5, servletPath.indexOf("/", 6));
            String path = servletPath.substring(servletPath.indexOf("/", 6)+1);
            String appPath = ApplicationManager.getApplicationPath(appName);
//            Log.debug(this, "Res file: "+path+", app: "+appName+", path: "+appPath);
            servletContext.getRequestDispatcher(Constants.APPLICATION_FS_PATH+appPath+Constants.RES_APP_PATH+path).forward(request, response);
            return;
        }
        if(servletPath.startsWith("/net/")){
            String requestType = servletPath.substring(5);
//            Log.debug(this, "Network type: "+requestType);
            NetworkManager.acceptConnection(requestType, req,(HttpServletResponse) response);
            return;
        }
//        Log.debug(this, "process request: "+req.getServletPath());
    }

    /**
     * Return the filter configuration object for this filter.
     */
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
        if (filterConfig != null) {
//            if(!DBUtil.exportSchema(aFolder+"/db.export")){
//                return false;
//            }
//            if(!DBUtil.createSchema()){
//                return;
//            }
            this.servletContext = filterConfig.getServletContext();
            SystemManager.init(servletContext);
        }
    }

    public void destroy() {
        SystemManager.destroy();
    }
}
